﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.LookAndFeel;
using System.Linq;
using BLL;
using System.IO;

namespace CRM.GUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        [STAThread]
        
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            UserLookAndFeel.Default.SetSkinStyle("Office 2007 Blue");
            SetConnection();
            ReadConfig();
            Ssl.EnableTrustedHosts();
            Application.Run(new CRM.GUI.HeThong.frmLogin());
        }

        static void ReadConfig()
        {
            System.Configuration.Configuration config =
                 ConfigurationManager.OpenExeConfiguration
                            (System.Windows.Forms.Application.ExecutablePath);
            string[] a = ConfigurationManager.AppSettings.AllKeys;

            #region RecordStorage
            if (!a.Contains("SaveCallFilePath"))
            {
                string filepath = System.Windows.Forms.Application.StartupPath + @"\CallFile";
                Directory.CreateDirectory(filepath);
                config.AppSettings.Settings.Add("SaveCallFilePath", filepath + @"\");
            }
            else
            {
                string filepath = System.Windows.Forms.Application.StartupPath + @"\CallFile";
                config.AppSettings.Settings["SaveCallFilePath"].Value = filepath + @"\";
            }

            if (!Directory.Exists(config.AppSettings.Settings["SaveCallFilePath"].Value))
            {
                string filepath = System.Windows.Forms.Application.StartupPath + @"\CallFile";
                Directory.CreateDirectory(filepath);
                if (!a.Contains("SaveCallFilePath"))
                {
                    config.AppSettings.Settings.Add("SaveCallFilePath", filepath + @"\");
                }
                else
                {
                    config.AppSettings.Settings["SaveCallFilePath"].Value = filepath + @"\";
                }
            }

            if (!a.Contains("DownloadPath"))
            {
                config.AppSettings.Settings.Add("DownloadPath", @"https://10.68.65.11/modules/monitor/");
            }
            #endregion
            #region AsteriskConfig
            if (!a.Contains("DomainName"))
            {
                config.AppSettings.Settings.Add("DomainName", "10.68.65.11");
            }
            if (!a.Contains("Account"))
            {
                config.AppSettings.Settings.Add("Account", "trilm");
            }
            if (!a.Contains("Password"))
            {
                config.AppSettings.Settings.Add("Password", "password");
            }
            if (!a.Contains("Port"))
            {
                config.AppSettings.Settings.Add("Port", "5038");
            }
            if (!a.Contains("PrefixNum"))
            {
                config.AppSettings.Settings.Add("PrefixNum", "78");
            }
            #endregion
            #region Encrytion Key Config
            if (!a.Contains("SecurityKey"))
            {
                config.AppSettings.Settings.Add("SecurityKey", "123ng@ym@i");
            }
            #endregion
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            DTO.SharedData.HostName = config.AppSettings.Settings["DomainName"].Value;
        }

        static void SetConnection()
        {
            DatabaseFunc func = new DatabaseFunc();
            func.SetConnection();
        }

    }
}